﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DTO;
using BUS;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
namespace GUI
{
    public partial class FormBaoCaoDoanhThuTheoNam : Form
    {
        private class DoanhThuTheoNam
        {
            private int sSTT;
            private int sThangTrongNam;
            private long sDoanhThu;
            private double sTile;
            public int STT
            {
                get { return sSTT; }
                set { sSTT = value; }
            }
            public int ThangTrongNam
            {
                get { return sThangTrongNam; }
                set { sThangTrongNam = value; }
            }
            public long DoanhThu
            {
                get { return sDoanhThu; }
                set { sDoanhThu = value; }
            }
            public double Tile
            {
                get { return sTile; }
                set { sTile = value; }
            }

        };
        int[] listThang ={1,2,3,4,5,6,7,8,9,10,11,12};
        public FormBaoCaoDoanhThuTheoNam()
        {
            InitializeComponent();
            HoaDonBUS hdBus = new HoaDonBUS();
            comboBox2.DataSource = hdBus.DanhSachNam();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                button1.Enabled = true;
            }
            else
            {
                button1.Enabled = false;
            }
        }
        string path = "";
        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "Chọn nơi lưu";
            sfd.Filter = "Excel WorkBook|*.xls";
            sfd.FileName = "Bao Cao.xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                path = sfd.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // Cần Ghi Báo Cáo Ra FileExcel cho nên listDoanhThu sẽ để toàn cục
        List<DoanhThuTheoNam> listDoanhThu = new List<DoanhThuTheoNam>();
        long TongDoanhThu = 0;
        public void GhiBaoCaoRaFileExcel(string FileName)
        {
            if (File.Exists(FileName))
            {
                File.Delete(FileName);
            }
            Excel._Application oApp;
            Excel.Worksheet oSheet;
            Excel.Workbook oBook;
            oApp = new Excel.Application();
            oBook = (Excel.Workbook)oApp.Workbooks.Add();
            oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
            // Tiểu Đề
            oSheet.Cells[1, 1] = "STT";
            oSheet.Cells[1, 2] = "Tên Loại Món Ăn";
            oSheet.Cells[1, 3] = "Doanh Thu";
            oSheet.Cells[1, 4] = "Tỉ Lệ(%)";
            int i = 2;
            foreach (DoanhThuTheoNam singleR in listDoanhThu)
            {
                oSheet.Cells[i, 1] = singleR.STT;
                oSheet.Cells[i, 2] = singleR.ThangTrongNam;
                oSheet.Cells[i, 3] = singleR.DoanhThu;
                oSheet.Cells[i, 4] = singleR.Tile;
                i++;

            }
            oBook.SaveAs(FileName);
            oBook.Close();
            oApp.Quit();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            int nam = Convert.ToInt32 (comboBox2.SelectedValue.ToString());
            for (int i = 0; i < listThang.Length;i++ )
            {
                DoanhThuTheoNam singleRecord = new DoanhThuTheoNam();
                TinhTienBUS tt = new TinhTienBUS();
                singleRecord.STT = i + 1;
                singleRecord.DoanhThu = tt.DoanhThuTheoNam(listThang[i],nam);
                singleRecord.ThangTrongNam = listThang[i];
                singleRecord.Tile = 0;
                listDoanhThu.Add(singleRecord);
            }
            foreach (DoanhThuTheoNam singleRecord in listDoanhThu)
            {
                TongDoanhThu = TongDoanhThu + singleRecord.DoanhThu;
            }
            foreach (DoanhThuTheoNam singleRecord in listDoanhThu)
            {
                singleRecord.Tile = Math.Round((double)(singleRecord.DoanhThu * 100) / TongDoanhThu, 2);
            }
            foreach (DoanhThuTheoNam singleRecord in listDoanhThu)
            {
                dataGridView1.Rows.Add(singleRecord.STT, singleRecord.ThangTrongNam, string.Format("{0:#,##0 VND;(#,##0 VND);''}", singleRecord.DoanhThu), singleRecord.Tile);
            }
            textBox1.Text = string.Format("{0:#,##0 VND;(#,##0 VND);''}",TongDoanhThu);
            if (checkBox1.Checked)
            {
                GhiBaoCaoRaFileExcel(path);

            }
            MessageBox.Show("Báo Cáo Thành Công");
            listDoanhThu.Clear();
            TongDoanhThu = 0;
            return;
        }

        private void FormBaoCaoDoanhThuTheoThang_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
